home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Devices / PACKman C / PACKMan.c < prev    next >
Encoding:
Text File  |  1994-10-28  |  4.7 KB  |  188 lines  |  [TEXT/MPS ]

  1. //--------------------------------------------------------------------------
  2. //                                                                            
  3. //        PACKman                                                    
  4. //            by Scott “Zz” Zimmerman & Nick Thompson                                                    
  5. //                                                                            
  6. //        Description:    This snippet shows how to implement a simple
  7. //                        Chooser Package, updated from Scott “Zz” Zimmerman's 
  8. //                        PACKman pascal sample                                        
  9. //                                                                            
  10. //        Version:        1.0 Completed 10/19/94                                
  11. // 
  12. //
  13. //    Copyright:    © 1989-94 by Apple Computer, Inc., all rights reserved.
  14. //
  15. // Modification History:
  16. //
  17. //    10/18/94        nick    converted from Pascal
  18. //    10/19/94        nick    updated to reflect info in IM: Devices (pages 1-42 ff)
  19. //                            this sample is think C specific
  20. //
  21. // To do:
  22. //        implement for metrowerks and MPW C
  23. //--------------------------------------------------------------------------
  24.  
  25.  
  26. #define    CHOOSERLISTSTRS        1024
  27.  
  28. pascal OSErr MyPackage( short message, short caller, StringPtr objName, StringPtr objectName, long p1, long p2)
  29. {
  30.     OSErr            theError ;        // store errors in here
  31.     
  32.     Str255            tempStr1, tempStr2 ;
  33.     
  34.     Str255            mainDevStr ;    // read in from resources
  35.     Str255            monitorStr ;
  36.     
  37.     short            index ;
  38.     
  39.     Cell            cellLoc;
  40.     short            deviceCount;
  41.     Handle            selectedItem;
  42.     
  43.     SysEnvRec        sysInfo;
  44.     GDHandle        theDevice;
  45.     
  46.     theError = noErr;
  47.  
  48.     switch( message ) {
  49.     
  50.         case newSelMsg: 
  51.             // Called to record the newly selected device. 
  52.             DebugStr("\pnewSel" ) ;
  53.             selectedItem = GetResource('STR ', -4099);
  54.             if( selectedItem != nil ) 
  55.             {
  56.                 cellLoc.h = 0 ;
  57.                 cellLoc.v = 0 ;
  58.                 if(  LGetSelect(true, &cellLoc, (ListHandle)p1 ) ) {
  59.                     (**((short **)selectedItem)) = cellLoc.v ;
  60.                 }
  61.                 else {
  62.                     (**((short **)selectedItem)) = 0 ;
  63.                 }
  64.                 
  65.                 ChangedResource( selectedItem );
  66.                 WriteResource( selectedItem );
  67.             }
  68.             break ;
  69.         
  70.         case fillListMsg: 
  71.             
  72.             // Count the gDevices (monitors) available on this Macintosh.            
  73.             deviceCount = 1 ;
  74.     
  75.             if((theError = SysEnvirons(1, &sysInfo)) == noErr ) 
  76.             {
  77.                 // Okay, we got the system info, now see    
  78.                 // if we have Color Quickdraw.                
  79.                 if( sysInfo.hasColorQD )  {
  80.                     
  81.                     // Okay, we have CQD, now how many monitors are connected?                    
  82.                     theDevice = GetDeviceList();
  83.                     while( (**theDevice).gdNextGD != nil ) {
  84.                         deviceCount++;
  85.                         theDevice = GetNextDevice(theDevice);
  86.                     }
  87.                     
  88.                     // Okay, p1 is the device list handle.    
  89.                     // So call the List Mangler to add some    
  90.                     // items.    
  91.                                                 
  92.                     LAddRow(deviceCount, 0, (ListHandle)p1 ) ;
  93.                     cellLoc.h = 0 ;
  94.                     cellLoc.v = 0 ;
  95.                     
  96.                     // load the strings we are going to use
  97.                     GetIndString( mainDevStr, CHOOSERLISTSTRS, 1 ) ;
  98.                     GetIndString( monitorStr, CHOOSERLISTSTRS, 2 ) ;
  99.                     
  100.                     for( index = 1; index <= deviceCount; index++ ) {
  101.                     
  102.                         if( index == 1 ) 
  103.                         {
  104.                             cellLoc.v = 0 ;
  105.                                 
  106.                             LSetCell(&mainDevStr[1], mainDevStr[0], cellLoc, (ListHandle)p1 ) ;
  107.                             
  108.                         } 
  109.                         else {
  110.     
  111.                             // get the device number converted to a string
  112.                             NumToString(index, tempStr1) ;
  113.                             
  114.                             // copy the monitor string into the other temp string
  115.                             BlockMove( &monitorStr[1], &tempStr2[1], monitorStr[0] ) ;
  116.                             
  117.                             // and adjust the length byte
  118.                             tempStr2[0] = monitorStr[0];
  119.                             
  120.                             // concatenate our monitor string and the monitor number
  121.                             BlockMove( &tempStr1[1], &tempStr2[ tempStr2[0] + 1 ], tempStr1[0] ) ;
  122.         
  123.                             // and adjust the length byte
  124.                             tempStr2[0] += tempStr1[0] ;
  125.                             
  126.                             cellLoc.v = index - 1;
  127.                             LSetCell(&tempStr2[1], tempStr2[0], cellLoc, (ListHandle)p1 ) ;
  128.                         }
  129.                     }
  130.                 }
  131.             }
  132.             break ;
  133.     
  134.     // This message is sent when the Chooser wants us to reselect the    
  135.     // device that was selected the last time our PACK was used.  The    
  136.     // ID of this item is stored in a string resource.                    
  137.     case getSelMsg: 
  138.         cellLoc.h = 0;
  139.         cellLoc.v = 0;
  140.         selectedItem = GetResource('STR ', -4099) ;
  141.         if(  selectedItem != nil ) 
  142.         {
  143.         
  144.             while( LNextCell(false, true, &cellLoc, (ListHandle)p1) ) {
  145.             
  146.                 if(  cellLoc.v == (**((short **)selectedItem)) )
  147.                     LSetSelect(true, cellLoc, (ListHandle)p1) ;
  148.                 else
  149.                     LSetSelect(false, cellLoc, (ListHandle)p1);
  150.             }
  151.         } 
  152.         else {
  153.         
  154.             selectedItem = NewHandle(sizeof(short));
  155.             if(  selectedItem != nil ) {
  156.                 (**((short **)selectedItem)) = cellLoc.v;
  157.                 AddResource(selectedItem, 'STR ', -4099, "\p" );
  158.                 WriteResource(selectedItem);
  159.             }
  160.             LSetSelect(true, cellLoc, (ListHandle)p1) ;
  161.         }
  162.         break ;
  163.     
  164.     // Ignore these for now, since we don't know what they do, and it    
  165.     // doesn't appear that we need them.                                
  166.     //
  167.     case selectMsg: 
  168.         DebugStr("\pselectMsg" );
  169.         break;
  170.         
  171.     case deselectMsg: 
  172.         DebugStr("\pdeselectMsg" );
  173.         break;
  174.         
  175.     case terminateMsg: 
  176.         DebugStr("\pterminateMsg" );
  177.         break;
  178.         
  179.     case buttonMsg: 
  180.         DebugStr("\pbuttonMsg" );
  181.         break;
  182.     
  183.     }
  184.  
  185.     return theError;
  186.  
  187. }
  188.